#!/bin/bash

# stop on first error
set -e
set -u

echo "start testing ... "
echo "TRAVIS_BRANCH: $TRAVIS_BRANCH"
echo "USER: $USER"
echo "DB: $DB"

echo "--------- starting services ----------- "
service susan-dir start
service susan-sd start
service susan-fd start
sleep 10

echo "--------- checking services ----------- "
service susan-dir status
service susan-sd status
service susan-fd status


# enable bash debug
set -v

BACKUP_TEST_FILE=/usr/sbin/susan.test

echo "----- create some file to test backup / restore ----"
echo "susan restore test" > ${BACKUP_TEST_FILE}
echo
echo -e "status dir" | bconsole
echo
#echo "---- label a volume ----"
#echo -e "label volume=testvol pool=Full" | bconsole
#echo
echo "------ trigger backup job -----"
echo -e "run job=backup-susan-fd yes\rwait" | bconsole | grep "Job queued. JobId="
echo "status dir" | bconsole
echo
echo "------ trigger restore job -----"
echo -e "restore select current\r2\rls\rmark usr\rdone\ryes\rwait" | bconsole
echo "status dir" | bconsole
grep "susan restore test" /tmp/susan-restores/${BACKUP_TEST_FILE}

if [ "${BUILD_WEBUI:-}" ]; then
    # show environment
    export | grep " TRAVIS"

    service apache2 restart
    #service apache2 status
    export SUSAN_BROWSER="none"
    export SUSAN_USERNAME="citest"
    export SUSAN_PASSWORD="citestpass"
    export SUSAN_CLIENT_NAME="$HOSTNAME-fd"
    echo "--------- testing webui over selenium -----------"
    echo "configure add console name=citest password=citestpass profile=webui-admin" | bconsole
    python ${TRAVIS_BUILD_DIR}/webui/tests/selenium/webui-selenium-test.py -v
fi
